import { store } from "@/store";
import { defineStore } from "pinia";
import ForumAPI, { type ForumListQuery, type ForumForm } from "@/api/forum/forum";

export const useForumStore = defineStore("forum", {
  state: () => ({
    forumList: [] as ForumForm[],
    loading: false,
  }),
  actions: {
    async fetchForumList(params?: ForumListQuery) {
      this.loading = true;
      try {
        const response = await ForumAPI.getForumList(params);
        if (response) {
          this.forumList = response.records;
        }
      } finally {
        this.loading = false;
      }
    },
    async addForum(data: ForumForm) {
      await ForumAPI.addForum(data);
      await this.fetchForumList();
    },
    async updateForum(data: ForumForm) {
      await ForumAPI.updateForum(data);
      await this.fetchForumList();
    },
    async deleteForum(id: number) {
      await ForumAPI.deleteForum(id);
      await this.fetchForumList();
    },
  },
});

export function useForumStoreWithOut() {
  return useForumStore(store);
}
